#ifndef ZT_EM9118_FUN_H
#define ZT_EM9118_FUN_H
#include "zt_type.h"

#define EM9106_DEVTYPE                  0x9106

#define MAX_DEV_NUM						6
#define OSC_FREQ						36000000.0
#define PWM_FREQ						4000000.0
#define AD_HFIFO_HALF_COUNT             8192

#define AD_CH_COUNT						18
#define CT_CH_COUNT						4
#define EC_CH_COUNT						2
#define PWM_CH_COUNT                    2
#define DO_CH_COUNT                     2
#define DA_CH_COUNT						2
//#define DA_MAX_CODE						4095
//#define DA_MIN_CODE						0

//address offset
#define ADDR_ADSOFTSTART               9
//#define ADDR_ADDATA                    2
#define ADDR_FIFODATA                  3
#define ADDR_ADRANGE                   4
#define ADDR_ADGAIN                    11
#define ADDR_DACH                      15
#define ADDR_DACODE                    16
#define ADDR_DACTRL                    17
#define ADDR_DAEN                      18
#define ADDR_DAFIFO                    19
#define ADDR_DAFIFOCNT                 19
#define ADDR_DAFREQDIV                 94
#define ADDR_DARANGE                   96
#define ADDR_ADCH1T16                  57
#define ADDR_ADCH17T18                 58
#define ADDR_IOCH 		                59
#define ADDR_CTCH 		                60
#define ADDR_ECCH 		                61
//#define ADDR_PSYNC                     72
#define ADDR_FIFOSTATUS                10
//#define ADDR_RESET                     1
//#define ADDR_HFIFOCLR                  1
//#define ADDR_WFZ                       8
//#define ADDR_DAREG                     9
//#define ADDR_DATRIG                    9
#define ADDR_CTRLCODE                  10
#define ADDR_ID                        16
#define ADDR_ETRGROUPCNT_L             20
#define ADDR_ETRGROUPCNT_H             21

#define ADDR_CHFREQDIV_BASE            100
#define ADDR_GROUPFREQDIV_L            1
#define ADDR_GROUPFREQDIV_H            2

#define ADDR_SINGLE_AD_DATA_BASE       109

#define ADDR_DI1TO4                    12
#define ADDR_DO1TO2                    13
#define ADDR_IO1TO16                   12 //EM9106
#define ADDR_IO17TO24                  13 //EM9106
#define ADDR_DIR                       15 //EM9106
#define ADDR_IOCTRL                    14
#define ADDR_IOINFIFO                  59

#define ADDR_CT1L                      23
#define ADDR_CT1H                      24
#define ADDR_CTG1L                     35
#define ADDR_CTG1H                     36
#define ADDR_CTCTRL                    47
#define ADDR_CTINFIFO                  60
#define ADDR_CTOVER                    48

#define ADDR_EC1L                      80 
#define ADDR_EC1H                      81 
//#define ADDR_ECZ1L                     82 
//#define ADDR_ECZ1H                     83 
#define ADDR_EC2L                      84 
#define ADDR_EC2H                      85 
//#define ADDR_ECZ2L                     86 
//#define ADDR_ECZ2H                     87 
/*#define ADDR_EC3L                      97 
#define ADDR_EC3H                      98 
#define ADDR_ECZ3L                     99 
#define ADDR_ECZ3H                     100
#define ADDR_EC4L                      101
#define ADDR_EC4H                      102
#define ADDR_ECZ4L                     103
#define ADDR_ECZ4H                     104*/
#define ADDR_ECCLEAR                   79

#define ADDR_PWMEN                 75
#define ADDR_PWMPHASE              76
#define ADDR_PWMT0                 88 
#define ADDR_PWMW0                 89 
#define ADDR_PWMS0                 90

#define ADDR_ROM_ADDRESS               5
#define ADDR_ROM_DATA                  6
#define ADDR_ROM_RW                    7
#define ADDR_ROM_STA                   8

#define IRQ_ADFIFO                     0x20
#define IRQ_DAFIFO                     0x40

//AD
//#define AD_CH_AUTO                     0x40
#define AD_HFIFO_FULL                  0x1000
#define AD_HFIFO_HALF                  0x2000
#define AD_HFIFO_EMPTY                 0x4000
//#define AD_FINISHED                    0x8000
//#define AD_GAIN16TO32MASK              0x300

#define ZT_SUCCESS                     0
#define ZT_FALSE                       -1
//Error code
#define ERR_ADFIFO_FULL                1
#define ERR_ADFIFO_NOHALF              2
#define ERR_ADFIFO_HALF                3
#define ERR_OUT_PORT                   4
#define ERR_IN_PORT                    5
#define ERR_ADSFIFO_ALLOC              6
#define ERR_ADREADNUMOVER              7
#define ERR_ADReadSFifo                8

#define RANGE_AD_CNT                   4
#define RANGE_DA_CNT                   5

//ZeroFull ROM ADDR
//#define ROMADDR_DA0_RANGE0             0
//#define ROMADDR_DA0_RANGE0_MAX         0//最大值2个字节
//#define ROMADDR_DA0_RANGE0_MIN         2//最小值2字节
//#define ROMADDR_DA1_RANGE0             4
//#define ROMADDR_DA2_RANGE0             8
//#define ROMADDR_DA3_RANGE0             12
//#define ROMADDR_DARANGE0_SIZE          16
//#define ROMADDR_DARANGE_SIZE           (RANGE_DA_CNT * ROMADDR_DARANGE0_SIZE)
#define ROMADDR_AD1_RANGE0_WZ           0  //第一路±10V零点值
#define ROMADDR_AD1_RANGE0_WF           2  //第一路±10V满度值
#define ROMADDR_AD_RANGE0_SIZE          4  //每个AD单个范围占用空间
#define ROMADDR_AD1_RANGE1_WZ           4  //第一路±5V零点值
#define ROMADDR_AD1_RANGE1_WF           6  //第一路±5V满度值
#define ROMADDR_AD1_RANGE2_WZ           8  //第一路0~20mA零点值
#define ROMADDR_AD1_RANGE2_WF           10 //第一路0~20mA满度值
#define ROMADDR_AD_RANGE_SIZE           12  //每个AD范围占用空间
#define ROMADDR_AD_ALL_RANGE_SIZE       (AD_CH_COUNT*ROMADDR_AD_RANGE_SIZE) //所有AD范围占用空间

int EM9118_Init();

int EM9118_ROMReadByte( int fd, int addr, U8* data );
int EM9118_ROMWriteByte( int fd, int addr, U8 data );

//操作9G45的设备空间
int EM9118_WriteM( int fd, U32 ulAddr, U32 ulData );
U32 EM9118_ReadM( int fd, U32 ulAddr );
//字方式直接向偏移地址为ulAddr的端口写入ulData
int EM9118_Outw( int fd, U32 ulAddr, U32 ulData );
//字方式直接从偏移地址为ulAddr的端口读回数据
U16 EM9118_Inw( int fd, U32 ulAddr );
//字节方式直接向偏移地址为ulAddr的端口写入ulData
int EM9118_Outb( int fd, U32 ulAddr, U32 ulData );
//字节方式直接从偏移地址为ulAddr的端口读回数据
U8 EM9118_Inb( int fd, U32 ulAddr );

//返回端口状态
U32 EM9118_IODiAll( int fd );
//在输出情况下，设置端口状态
I32 EM9118_IODoAll( int fd, U32 ulStateAll );

//读回软件缓冲区中AD数据量
U32 EM9118_ADSFifoCanReadNumber( int fd );
//从AD缓冲区中读回指定个数的数据，注意pCode的大小必须大于或等于itemsCount*2
I32 EM9118_ADReadSFifo( int fd, U16* pCode, U32 itemsCount );

I32 EM9118_ADReadOnceCode( int fd, I32 delayUS, U16 adCode[AD_CH_COUNT] );
U16 EM9118_ADReadSingle( int fd, int ch );
//设置AD通道使能位
I32 EM9118_ADSetCh( int fd, U32 enCh );
U32 EM9118_ADGetCh( int fd );
I32 EM9118_ADSetRangeAll( int fd, U16 rangeInx );
I32 EM9118_ADSetGainAll( int fd, U16 gainInx );
I32 EM9118_ADGetGainAll( int fd, U16* gainInx );

U32 EM9118_CTRead( int fd, int chInx );
int EM9118_CTSetModeAll( int fd, U16 ctMode );
U8 EM9118_CTGetMode( int fd, int chInx );
int EM9118_CTSetIClkDiv( int fd, int chInx, U32 div );
int EM9118_CTOver( int fd, int chInx );//这个函数是为了判断测频是否成功
int EM9118_CTClearAll( int fd );
int EM9118_CTInFifo(int fd, U16 isFifo[CT_CH_COUNT]);

U8 EM9118_IOReadGroup(int fd, int groupInx);
int EM9118_IOWriteGroup( int fd, int groupInx, U8 od );
int EM9118_IOInFifo(int fd, int isFifo);
void EM9118_IOSetMode( int fd, U16 doMode[DO_CH_COUNT] );//设置DO管脚的作用
I32 EM9118_StateToDo( int fd,  int stateId, int value);

U32 EM9118_SFifoResize( int fd, int chCount );

int EM9118_RomWZWrite( int fd, int chInx, int rangeInx, I16 wz );
int EM9118_RomWFWrite( int fd, int chInx, int rangeInx, I16 wf );
int EM9118_RomWFWZWrite( int fd, int chInx, int rangeInx, I16 wz, I16 wf );
int EM9118_RomWFWZRead( int fd, int chInx, int rangeInx, I16* wz, I16* wf );

U32 EM9118_ECRead( int fd, int chInx );
int EM9118_ECClear( int fd, int chInx );

I32 EM9118_HFifoClear( int fd );
I32 EM9118_HCSetChFreq( int fd, U16* freqDiv );
I32 EM9118_HCSetGroupFreq( int fd, U32 groupFreqDiv );
I32 EM9118_HCStart( int fd, U32 startMode );
I32 EM9118_HCStop( int fd );
I32 EM9118_HCSetETRGroupCount( int fd, U32 groupCount );

int EM9118_GetSysErrorCode( int fd );//ZCD 13-10-15

void EM9118_FeedDog( int fd );//ZCD 13-10-25

int EM9118_PWMSetFreq( int fd, int chInx, U16 t );
int EM9118_PWMSetWide( int fd, int chInx, U16 w );
int EM9118_PWMSetStepCnt( int fd, int chInx, U16 stepCnt );
int EM9118_PWMSetPhaseAll( int fd, U8 phase );
int EM9118_PWMEnableAll( int fd, U8 e );
U8 EM9118_PWMIsOver( int fd );

I32 EM9118_ReadHFifo( int fd, U32 overTimeUS, U16 codeCount, U16* adCode );

I32 EM9118_IRQEnable( int fd, U32 irqInx );
I32 EM9118_IRQDisable( int fd );

I32 EM9118_GetDeviceType( int fd );


I32 EM9106_IOSetDir( int fd, U32 dir );
I32 EM9106_IODoAll( int fd, U32 ulStateAll );
U32 EM9106_IODiAll( int fd );

I32 EM9106_DaSetCtrl( int fd, int ctrlCode );
I32 EM9106_DaSetCodeOnce( int fd, int chInx, int daCode );
I32 EM9106_DaSetRange( int fd, U16 rangeCode );
I32 EM9106_DaSetEnCh( int fd, U16 enCh );
I32 EM9106_DaSetFreqDiv( int fd, U32 freqDiv );
U32 EM9118_DaSFifoClear( int fd );
U32 EM9118_DaSFifoCanReadNumber( int fd );
I32 EM9118_DaWriteSFifo( int fd, U16* pCode, U32 itemsCount );
I32 EM9118BD_AdIrqEnable( int fd, int irqEnable );

I32 EM9106BD_IOSetDir( int fd, int dirReg );

#endif
